import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

public class SearchPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTable searchTable;
	private SearchTableModel model;
	private JPanel leftPanel;
	private JTextField searchTextField;
	private JButton searchButton, refreshButton;

	public SearchPanel() {
		this.setLayout(new BorderLayout());
		leftPanel = new JPanel();
		leftPanel.setLayout(new GridBagLayout());
		model = new SearchTableModel();
		searchTable = new JTable(model);
		searchTable.setFillsViewportHeight(true);
		searchTable.setVisible(true);
		this.add(new JScrollPane(searchTable), BorderLayout.EAST);
		// lewa strona
		GridBagConstraints c = new GridBagConstraints();
		c.fill = GridBagConstraints.CENTER;
		c.gridx = 0;
		c.gridy = 0;
		searchTextField = new JTextField(12);
		leftPanel.add(searchTextField, c);
		c.gridy = 1;
		searchButton = new JButton("Search");
		searchButton.setVisible(true);
		leftPanel.add(searchButton, c);
		leftPanel.setVisible(true);

		c.fill = GridBagConstraints.NORTH;
		c.gridx = 0;
		c.gridy = 50;
		refreshButton = new JButton("Refresh");
		refreshButton.setVisible(true);
		addSearchListener();
		addRefreshListener();
		leftPanel.add(refreshButton, c);

		this.add(leftPanel, BorderLayout.WEST);

	}
	
	private void addSearchListener(){
		searchButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.out.println("wyszukiwanie ...");
				Client.getMainFr().getProg().updatePeers();
				Client.getMainFr().findFile(searchTextField.getText(), getModel().getData());
				searchTable.repaint();
			}
		});
	}

	private void addRefreshListener() {
		refreshButton.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				Client.getMainFr().getProg().updatePeers();
				Client.getMainFr().updateFileList(
						Client.getMainFr().getProg().getPeers(),
						model.getData());
				searchTable.repaint();
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}
		});

		searchTable.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				// TODO Auto-generated method stub
				if (arg0.getClickCount() == 2) {
					String name = new String();
					int breakFlag = 0;
					int row = searchTable.getSelectedRow();
					if ((name = (String) searchTable.getModel().getValueAt(row,
							0)) != null) {
						//System.out.println("wanna download? :> " + name);

						for (int i = 0; i < Client.getMainFr().getProg().getPeers().size(); i++) {
							if (breakFlag == 1) {
								break;
							}
							if (getRowContent(row, 2).compareTo(
									getPeer(i).getName()) == 0) {
								if (getRowContent(row, 3).compareTo(
										getPeer(i).getIP().toString()) == 0) {
									if (getRowContent(row, 4).compareTo(
											"" + getPeer(i).getListenPort()) == 0) {
										for (int j = 0; j < getPeer(i).getFiles().size(); j++) {
											if (name.compareTo(getPeer(i).getFiles().get(j).getName()) == 0) {
												if (getRowContent(row, 1).compareTo(""+ getPeer(i).getFiles().get(j).getSize()) == 0) {
													//System.out.println("znaloz zem");
													Runnable cl = new ClThread(getPeer(i), getPeer(i).getFiles().get(j));
													Thread t = new Thread(cl);
													t.start();
													breakFlag = 1;
													break;
												}
											}
										}
									}
								}
							}
						}

					}
				}
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
				// TODO Auto-generated method stub

			}

		});
	}

	public void removeEl() {
		for (int i = 0; i < 100; i++)
			for (int j = 0; j < 5; j++)
				model.getData()[i][j] = "";
		searchTable.repaint();
	}

	public String getRowContent(int row, int col) {
		return searchTable.getModel().getValueAt(row, col).toString();
	}

	public Peer getPeer(int i) {
		return Client.getMainFr().getProg().getPeers().get(i);
	}

	public SearchTableModel getModel() {

		return model;
	}

}
